<template>
  <div class="uploadimg">
    <el-upload
      v-if="!imgUrl"
      action=""
      list-type="picture-card"
      :before-upload="beforeUpload"
      :http-request="httpRequest"
      :show-file-list="false"
    >
      <i class="el-icon-upload"></i>
    </el-upload>

    <div class="imagepreview" v-else>
      <el-image class="myImg" :src="imgUrl" :preview-src-list="[imgUrl]">
        <template slot="error">
          <el-image
            src="https://lev-1306481888.cos.ap-nanjing.myqcloud.com/bigUserHeader.png"
            class="errImg"
          ></el-image>
        </template>
      </el-image>
      <i class="el-icon-close" @click="cancel"></i>
    </div>
    <el-progress
      v-if="showP"
      :show-text="false"
      :stroke-width="3"
      :percentage="percentage"
    ></el-progress>
  </div>
</template>

<script>
import COS from "cos-js-sdk-v5";
export default {
  neme: "uploadimg",
  props: {
    imgUrl: {
      type: String,
      required: true,
      default: "https://lev-1306481888.cos.ap-nanjing.myqcloud.com/bigUserHeader.png",
    },
  },
  data() {
    return {
      cos: null,
      percentage: "",
      showP: false,
    };
  },
  methods: {
    //
    // 上传文件成功之后
    httpRequest(file) {
      const myfile = file.file;
      console.log(myfile);
      this.cos.putObject(
        {
          Bucket: "lev-1306481888" /* 必须 */,
          Region: "ap-nanjing" /* 存储桶所在地域，必须字段 */,
          Key: myfile.name /* 必须 */,
          StorageClass: "STANDARD",
          Body: myfile, // 上传文件对象
          onProgress: (progressData) => {
            this.showP = true;
            this.percentage = progressData.percent * 100;
            // 上传过程中的实时进度对象
            // console.log(JSON.stringify(progressData))
            // console.log(progressData)
          },
        },
        (err, data) => {
          // 上传成功后的返回值
          // 上传出错的错误信息
          // console.log(err || data)
          if (err) {
            this.$message.error(err);
            this.showP = false;
          }
          this.$emit("update:imgUrl", "https://" + data.Location);
          // this.showP = false
          this.percentage = 100;
          setTimeout(() => {
            this.showP = false;
          }, 1000);
        }
      );
    },
    // 上传文件发送前
    beforeUpload(file) {
      const isImg = file.type === "image/jpeg" || file.type === "image/png";
      if (!isImg) {
        this.$message.error("上传图片格式不正确");
      }
      const less2M = file.size / 1024 / 1024 < 2;
      if (!less2M) {
        this.$message.error("上传图片不能超过2M");
      }
      return isImg && less2M;
    },

    // 删除图片
    cancel() {
      this.$emit("update:imgUrl", "");
    },
  },
  created() {
    this.cos = new COS({
      SecretId: "AKIDvd5GBpDJ8gxyiCPM2RZQ0JK5aokPownN", // 身份识别 ID
      SecretKey: "LO5VeX9UXKr75nEOGrCG3ZomTs1tTmoa", // 身份密钥
    });
  },
};
</script>

<style lang="scss" scoped>
.uploadimg {
  width: 150px;
  height: 150px;
  .imagepreview {
    position: relative;
    background-color: rgb(243, 243, 243);
    .myImg,.errImg {
      width: 100%;
      border: 1px dashed #c0ccda;
      text-align: center;
      border-radius: 5px;
      object-fit: contain;
      vertical-align: middle;
    }
    .errImg{
      border:none
    }
    i {
      display: none;
      position: absolute;
      top: 0;
      right: 0;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 20px;
      background-color: rgb(243 243 143 / 0.5);
      border-radius: 0 5px 0 20px;
      &:hover {
        cursor: pointer;
      }
    }
    &:hover i {
      display: block;
    }
  }
}
</style>